package org.juggers.client.ui;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Collection;

import org.juggers.client.JuggersMessages;
import org.juggers.client.controller.EventController;
import org.juggers.client.controller.JuggerController;
import org.juggers.client.ui.component.EventPanel;
import org.juggers.domain.Event;
import org.juggers.domain.ModelState;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

/**
 * Main view
 * 
 * @author <a href="mailto:michel@breizhjug.org">Michel DAVID</a>
 */
@Singleton
public class MainPanel
    extends Composite
{

    @Inject
    private Provider<JuggerController> juggerControllerProvider;
    
    @Inject
    private Provider<EventController> eventControllerProvider;
    
    @SuppressWarnings( "unused" )
    @Inject
    private Menu menu;

    @Inject
    private MainPanel( final ModelState model, final JuggersMessages messages, ConnectionPanel connectionPanel )
    {
        VerticalPanel vPanel = new VerticalPanel();
        initWidget( vPanel );

        RootPanel cnxPanel = RootPanel.get( "cnx" );
        cnxPanel.add( connectionPanel );
        
        model.addPropertyChangeListener( "events", new PropertyChangeListener()
        {
            
            @SuppressWarnings( "unchecked" )
            @Override
            public void propertyChange( PropertyChangeEvent evt )
            {
                Collection<Event> events = (Collection<Event>) evt.getNewValue();
                
                RootPanel pagePanel = RootPanel.get( "page" );
                pagePanel.clear();
                
                for ( final Event event : events )
                {
                    EventPanel panel = new EventPanel( eventControllerProvider.get(), messages );
                    panel.setEvent( event );
                    pagePanel.add( panel );
                }
            }
        });
    }

    protected void onLoad()
    {
        super.onLoad();
        
        eventControllerProvider.get().showUpcomingEvents();

        // TODO : Trouver une solution plus elegante
        String token = Window.Location.getParameter( "token" );
        String email = Window.Location.getParameter( "email" );
        if ( token != null && email != null )
        {
            juggerControllerProvider.get().confirm( email, token );
        }
    }
}
